package com.smh.a02streamDemo1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.function.IntFunction;

public class StreamDemo5 {
    public static void main(String[] args) {
        //终结方法

        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"asd","asd","qwe","zxc","asd","zxc");

        //遍历
        list.stream().forEach(s -> System.out.println(s));
        System.out.println();
        //统计
        //个数
        System.out.println(list.stream().count());

        //收集
        Object[] array = list.stream().toArray();
        System.out.println(array);
        System.out.println("----------------------");

        //toArray方法的参数作用,创建一个指定类型的数组
        //方法的底层是依次得到流里面的每一个数据,并将数据放到数组当中
        //返回值是一个指定数组
        String[] array1 = list.stream().toArray(new IntFunction<String[]>() {

            //apply形参是流中数据的个数,数组的大小要与数据的个数保持一致
            @Override
            public String[] apply(int value) {
                return new String[value];
            }
        });
        for (String s : array1) {
            System.out.println(s);
        }
        System.out.println("--------------------");
        String[] array2 = list.stream().toArray(value -> new String[value]);

        for (String s : array2) {
            System.out.println(s);
        }


    }
}
